package leetcode;

import java.util.Arrays;

public class LC18 {
    public static void main(String[] args) {
        LC18 lc18 = new LC18();
        lc18.setZeroes(new int[][]{{1,0,1}});
        lc18.setZeroes(new int[][]{{1,1,1},{1,0,1},{1,1,1}});
        lc18.setZeroes(new int[][]{{1,2,3,4},{5,0,7,8},{0,10,11,12},{13,14,15,0}});
        lc18.setZeroes(new int[][]{{0,1,2,0},{3,4,5,2},{1,3,1,5}});
        lc18.setZeroes(new int[][]{{0,1,2,0},{3,0,5,2},{1,3,1,5}});
        lc18.setZeroes(new int[][]{{0,1,2,0},{3,4,5,2},{1,3,0,5}});
    }
    public void setZeroes(int[][] matrix) {
        int x = matrix[0].length;
        int y = matrix.length;
        boolean row0 = false, col0 = false;
        for (int i = 0; i < y; i++) {
            if (matrix[i][0] == 0) {
                col0 = true;
            }
        }
        for (int i = 0; i < x; i++) {
            if (matrix[0][i] == 0) {
                row0 = true;
            }
        }
        for (int i = y-1; i > 0; i--) {
            for (int j = x-1; j > 0; j--) {
                if (matrix[i][j] == 0){
                    matrix[i][0] = 0;
                    matrix[0][j] = 0;
                }
            }
        }
        for (int i = y-1; i > 0; i--) {
            for (int j = x-1; j > 0; j--) {
                if (matrix[0][j] == 0 || matrix[i][0] == 0){
                    matrix[i][j] = 0;
                }
            }
        }

        for (int i = 0; i < y; i++) {
            if (col0) matrix[i][0] = 0;
        }
        for (int i = 0; i < x; i++) {
            if (row0) matrix[0][i] = 0;
        }
        for (int i = 0; i < y; i++) {
            System.out.println(Arrays.toString(matrix[i]));
        }
        System.out.println("===============");
    }
}
